using EventBehaviorTree;
using System;
using System.Reflection;
using System.Xml;
using UnityEngine;
using UnityEngine.InputSystem;

namespace BehaviorSystem
{
    public static class BehaviorBuilder
    {
        public static void Reg_BehaviorInfo(string ownerType, BehaviorFilter behaviorFilter, Action<string> behaviorActivator)
        {
            XmlNodeList xnList = XmlHelper.GetXmlNodeList("BehaviorAssets.xml", $"/Behavior/{ownerType}/BehaviorInfo/Value");
            foreach (XmlNode xn in xnList)
            {
                behaviorFilter.Reg_BehaviorInfo(xn.Attributes["Key"].Value, new(xn.InnerText));
                if (xn.Attributes["Input"] != null)//register input information
                {
                    InputManager.Instance.Reg_InputEvent(
                        (GameplayKeyCode)Enum.Parse(typeof(GameplayKeyCode), xn.Attributes["Input"].Value),
                        InputActionPhase.Started, delegate { behaviorActivator(xn.Attributes["Key"].Value); });
                }
            }
        }

        public static void Reg_BehaviorNodes(object owner, StateSwitcher behaviorCentral)
        {
            XmlNodeList xnList = XmlHelper.GetXmlNodeList("BehaviorAssets.xml", $"/Behavior/{owner.GetType()}/CompositeBehavior/State");
            foreach (XmlNode xn in xnList)
            {
                Sequencer sequencer = new();
                behaviorCentral.AddSubNode(sequencer);
                foreach (XmlNode subNode in xn.ChildNodes)
                {
                    NodeBuilder.Gen_Nodes(subNode, sequencer, BindingFlags.NonPublic | BindingFlags.Instance, owner);
                }
            }
        }
    }
}
